package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.tests.GroupData;
import com.example.utils.ListOf;
import com.example.utils.SortedListOf;

import static com.example.fw.NavigationHelper.*;

public class ContactHelper extends WebDriverHelperBase {
	
	public static boolean CREATION = true;
	
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public ListOf<ContactData> getNoSortContacts(){
		ListOf<ContactData> contacts= new ListOf<ContactData>();
		manager.navigateTo().mainPage(GET_PAGE);
		List <WebElement> contactListForm = driver.findElements(By.xpath("//tr[@name = 'entry']"));
		for (WebElement contactList : contactListForm) {
	    	ContactData contact = new ContactData()
                .withFirstName(contactList.findElement(By.xpath("td[2]")).getText())
                .withLastName(contactList.findElement(By.xpath("td[3]")).getText())
	    		.withFirstEmail(contactList.findElement(By.xpath("td[4]")).getText())
	    		.withPhone(contactList.findElement(By.xpath("td[5]")).getText());
	    	contacts.add(contact);
		}
		return contacts;
	}

	public ContactHelper createContact(ContactData contact) {
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitCreationContact();
		returnToContactPage();
		manager.getModel().addContacts(contact);
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().mainPage(CLICK_LINK);
		openEditPage(index);
	    fillContactForm(contact, MODIFICATION);
	    submitContactModification();
	   	returnToContactPage();
	   	manager.getModel().removeContacts(index);
	   	return this;
	}
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage(CLICK_LINK);
		openEditPage(index);
	    submitContactDelete();
	    returnToContactPage();
	    manager.getModel().removeContacts(index);
	    return this;
	}
	
	//----------------------------------------------------------------------------------------------------
	
	public SortedListOf<ContactData> getUiContacts(){
		SortedListOf<ContactData> contact = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage(CLICK_LINK);	
		List <WebElement> contactListForm = driver.findElements(By.xpath("//tr[@name = 'entry']"));	
		for (WebElement checkbox : contactListForm) {
	    	contact.add(new ContactData()
	    	    .withFirstName(checkbox.findElement(By.xpath("td[2]")).getText())
	            .withLastName(checkbox.findElement(By.xpath("td[3]")).getText()));	    	
		}
		return contact;
	}	
	
	public ContactHelper returnToContactPage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper initContactCreation() {
		manager.navigateTo().contactCreationPage();
		return this;
	}

	public ContactHelper submitCreationContact() {
		click(By.name("submit"));
		return this;
	}

	public ContactHelper openEditPage(int index) {
		click(By.xpath("//tr[" + (index + 2) + "]/td[7]//img"));
		return this;
	}

	public ContactHelper submitContactDelete() {
		click(By.xpath("//input[@value = 'Delete']"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@value = 'Update']"));
		return this;
	}
	
	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getFirstAddress());
		type(By.name("home"), contact.getHomePhone());
		type(By.name("mobile"), contact.getMobilePhone());
		type(By.name("work"), contact.getWorkPhone());
		type(By.name("email"), contact.getFirstEmail());
		type(By.name("email2"), contact.getSecondEmail());
		selectByText(By.name("bday"), contact.getBDay());
		selectByText(By.name("bmonth"), contact.getBMonth());
		type(By.name("byear"), contact.getBYear());
		if (formType == CREATION) {
//			selectByText(By.name("new_group"), contact.group);
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0) {
				throw new Error("Group selector exists in contact modification form");
			}
		}		
		type(By.name("address2"), contact.getSecondAdderess());
		type(By.name("phone2"), contact.getPhone());
		return this;
	}
}
